﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hospital.Dao.IDao;
using Spring.Data.NHibernate.Generic.Support;
using Spring.Data.NHibernate.Generic;
using NHibernate;
using Hospital.Model;

namespace Hospital.Dao.DaoImpl
{
    public class AnnouncementDaoImpl : HibernateDaoSupport, IAnnouncementDao
    {
        #region IAnnouncementDao 成员

        public Hospital.Model.Announcement GetAnnouncementById(Guid gId)
        {
            return (Announcement)HibernateTemplate.Execute(new HibernateDelegate<Announcement>(delegate(NHibernate.ISession session)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("from Announcement  where Id=?");
                IQuery query = session.CreateQuery(sb.ToString());
                query.SetParameter(0, gId);
                return query.UniqueResult() as Announcement;
            }), true);
        }

        public IList<Hospital.Model.Announcement> GetAllAnnouncement()
        {
            return HibernateTemplate.LoadAll<Announcement>();
        }

        public IList<Hospital.Model.Announcement> GetAnnouncement(int iCount)
        {
            return (IList<Announcement>)HibernateTemplate.Execute(new HibernateDelegate<IList<Announcement>>(delegate(NHibernate.ISession session)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("from Announcement  order by PublishDate");
                IQuery query = session.CreateQuery(sb.ToString());
                query.SetMaxResults(iCount);
                return query.List<Announcement>();
            }), true);
        }

        public bool AddOrUpdateAnnouncementr(Hospital.Model.Announcement announce)
        {
            HibernateTemplate.SaveOrUpdate(announce);
            return true;
        }

        public bool DeleteAnnouncement(Hospital.Model.Announcement announce)
        {
            try
            {
                HibernateTemplate.Delete(announce);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }
}
